package com.hm.cms.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import com.hm.cms.domain.QTermTayRelationshipsDomain;
import com.hm.cms.domain.TermTayRelationshipsDomain;
import com.hm.cms.repository.TermTayRelationshipsRepository;
import com.hm.cms.service.TermTayRelationshipsService;
import com.hm.common.util.CommonUtil;

import lombok.extern.slf4j.Slf4j;

/**
 * @author shishun.wang
 * @date 2017年10月28日 上午11:11:21
 * @version 1.0
 * @describe
 */
@Slf4j
@Service
public class TermTayRelationshipsServiceImpl implements TermTayRelationshipsService {

	@Autowired
	private TermTayRelationshipsRepository termTayRelationshipsRepository;

	@Autowired
	private MongoTemplate mongoTemplate;

	/**
	 * 统计阅读量
	 * @param articleId
	 */
	@Override
	@Async
	public void statisticsViewQuantity(String articleId) {
		try{
			if (CommonUtil.isEmpty(articleId)) {
				return;
			}
			TermTayRelationshipsDomain relationshipsDomain = termTayRelationshipsRepository
					.findOne(QTermTayRelationshipsDomain.termTayRelationshipsDomain.articleId.eq(articleId));
			if (CommonUtil.isEmpty(relationshipsDomain)) {
				return;
			}

			Query query = Query.query(Criteria.where("article_id").is(articleId));
			mongoTemplate.updateFirst(query, new Update().inc("real_view_quantity", 1), TermTayRelationshipsDomain.class);
		}catch(Exception e){
			log.error(e.getMessage(),e);
		}
	}
	
	/**
	 * 统计评论数
	 * @param articleId
	 */
	@Override
	public void statisticsCommentQuantity(String articleId) {
		if (CommonUtil.isEmpty(articleId)) {
			return;
		}
		TermTayRelationshipsDomain relationshipsDomain = termTayRelationshipsRepository
				.findOne(QTermTayRelationshipsDomain.termTayRelationshipsDomain.articleId.eq(articleId));
		if (CommonUtil.isEmpty(relationshipsDomain)) {
			return;
		}

		Query query = Query.query(Criteria.where("article_id").is(articleId));
		mongoTemplate.updateFirst(query, new Update().inc("real_comment_quantity", 1), TermTayRelationshipsDomain.class);
	}
	
}
